<?php

namespace common\models;

use Yii;
use himiklab\sitemap\behaviors\SitemapBehavior;
use yii\helpers\Url;
/**
 * This is the model class for table "subject".
 *
 * @property integer $id
 * @property string $title
 * @property string $desc
 * @property string $img
 * @property string $url
 * @property integer $count
 * @property integer $status
 * @property integer $created_at
 */
class Subject extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'subject';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['count', 'status', 'created_at'], 'integer'],
            [['title', 'img', 'url'], 'string', 'max' => 100],
            [['desc'], 'string', 'max' => 200],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'title' => Yii::t('common', 'Title'),
            'desc' => Yii::t('common', 'Desc'),
            'img' => Yii::t('common', 'Img'),
            'url' => Yii::t('common', 'Url'),
            'count' => Yii::t('common', 'Count'),
            'status' => Yii::t('common', 'Status'),
            'created_at' => Yii::t('common', 'Created At'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPosts()
    {
        return $this->hasMany(Post::className(), ['cid' => 'id'])->inverseOf('c');
    }
	
    /**
     * 设置自动更新时间   created_at | updated_at 字段类型为 int(11) 
     */
    public function behaviors() {
        return [
            \yii\behaviors\TimestampBehavior::className(),
//            // 生成sitemap
//            'sitemap' => [
//                'class' => SitemapBehavior::className(),
//                'scope' => function ($model) {
//                    $model->select(['id', 'created_at'])->orderBy('id desc');
//                },
//                'dataClosure' => function ($model) {
//                    /** @var self $model */
//                    return [
//                        'loc' => Url::to('/subject/'.$model->id, true),
//                        'lastmod' => strtotime(date('Y/m/d h:i:s', $model->created_at)),
//                        'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY,
//                        'priority' => 0.6
//                    ];
//                }
//            ],            
        ];
    } 	
}
